package com.manor.main.adapter;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.viewpager.widget.PagerAdapter;

import com.bumptech.glide.Glide;
import com.github.chrisbanes.photoview.PhotoView;
import com.manor.common.CommonAppConfig;
import com.manor.common.http.HttpCallback;
import com.manor.common.utils.RouteUtil;
import com.manor.common.utils.StaIntentUtil;
import com.manor.common.utils.ToastUtil;
import com.manor.common.utils.WordUtil;
import com.manor.main.R;
import com.manor.main.bean.PhotoBean;
import com.manor.main.http.MainHttpUtil;

import java.util.List;


/**
 * @Description:
 * @Author: Liangchaojie
 * @Create On 2018/3/30 10:33
 */
public class ImagePreviewAdapter extends PagerAdapter {
    private Context context;
    private List<PhotoBean> imageList;
    private String mCoinName;


    public ImagePreviewAdapter(Context context, List<PhotoBean> imageList, int itemPosition) {
        this.context = context;
        this.imageList = imageList;
    }

    @Override
    public int getCount() {
        return imageList == null ? 0 : imageList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        View convertView = LayoutInflater.from(context).inflate(R.layout.page_image_preview, null);
        PhotoView image = convertView.findViewById(R.id.image);
        LinearLayout ll_view = convertView.findViewById(R.id.ll_view);
        TextView content = convertView.findViewById(R.id.content);
        TextView btn_cancel = convertView.findViewById(R.id.btn_cancel);
        TextView btn_confirm = convertView.findViewById(R.id.btn_confirm);
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                image.setEnabled(false);
                ((Activity) context).onBackPressed();
                RouteUtil.forwardPath(RouteUtil.PC_WEB, StaIntentUtil.ARISTOCRATIC_PRIVILEGE);
            }
        });
        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                buyPhoto(imageList.get(position).getId(), position, image);
            }
        });
        image.setEnabled(true);
        if (imageList.get(position).isPrivate()) {
            if (imageList.get(position).isCanSee()) {
                //    Glide.with(context).load(imageList.get(position).getThumb()).into(image);
                ll_view.setVisibility(View.GONE);
                Glide.with(context).load(imageList.get(position).getThumb()).thumbnail(0.6f).into(image);
            } else {
                if (TextUtils.isEmpty(mCoinName)) {
                    mCoinName = CommonAppConfig.getInstance().getCoinName();
                }
                Glide.with(context).load(R.mipmap.pic_blur).into(image);
                ll_view.setVisibility(View.VISIBLE);
                content.setText(String.format(WordUtil.getString(R.string.photo_privte_tip), imageList.get(position).getCoin(), mCoinName));
            }
        } else {
            //ImgLoader.display(context, imageList.get(position).getThumb(), image);
            ll_view.setVisibility(View.GONE);
            Glide.with(context).load(imageList.get(position).getThumb()).thumbnail(0.6f).into(image);
        }
        image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((Activity) context).onBackPressed();
            }
        });

        container.addView(convertView);
        return convertView;
    }

    private void buyPhoto(String id, int position, ImageView image) {
        MainHttpUtil.buyPhoto(id, new HttpCallback() {
            @Override
            public void onSuccess(int code, String msg, String info) {
                if (code == 0) {
                    imageList.get(position).setCansee("1");
                    ((Activity) context).onBackPressed();
                    // ImgLoader.display(context, imageList.get(position).getThumb(), image);
                } else if (code == 1005) {
                    ToastUtil.show(R.string.chat_coin_not_enough);
//                    RouteUtil.forwardMyCoin();
                    RouteUtil.forwardPath(RouteUtil.PC_WEB, StaIntentUtil.QUICK_RECHARGE);
                } else {
                    ToastUtil.show(msg);
                }
            }
        });
    }


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }


}
